﻿
#if 0

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "skfapi.h"
#include "skfapi_ex.h"
#include "ALGSm2_PR.h"
#include "sm4.h"


#define B  1
#define KB 1024
#define MB 1024*1024
#define MAXSIZE 1024*1024*100

#define	TRUE	1
#define FALSE	0
#define ERROR_THROW(r) {if((r) != SAR_OK) goto END_OF_FUN;}

//#define CONF_NAME "/data/jpx/data/conf.ini"
//#define CONF_PATH "/data/jpx/data"
//#define CONF_NAME "C:/Users/jpx/Desktop/VMKey_V1.0/发布/VMKey_V1.0_release1/VMKey_client_V1.0/data/conf.ini"
//#define CONF_PATH "C:/Users/jpx/Desktop/VMKey_V1.0/发布/VMKey_V1.0_release1/VMKey_client_V1.0/data"

char *get_algmod_print_buf(int alg)
{
	char buf[256] = { 0 };

	switch (alg)
	{
	case SGD_SM1_ECB:
		sprintf(buf, "SGDSM1ECB");
		break;
	case SGD_SM1_CBC:
		sprintf(buf, "SGDSM1CBC");
		break;
	case SGD_SM1_CFB:
		sprintf(buf, "SGDSM1CFB");
		break;
	case SGD_SM1_OFB:
		sprintf(buf, "SGD_SM1_OFB");
		break;

	case SGD_SM4_ECB:
		sprintf(buf, "SGDSM4ECB");
		break;
	case SGD_SM4_CBC:
		sprintf(buf, "SGDSM4CBC");
		break;
	case SGD_SM4_CFB:
		sprintf(buf, "SGDSM4CFB");
		break;
	case SGD_SM4_OFB:
		sprintf(buf, "SGDSM4OFB");
		break;

	default:
		break;
	}

	return buf;
}

int gen_random_test(HANDLE hdev, BYTE *pbData, ULONG ulDataLen)
{
	ULONG ulRslt = SAR_OK;
	time_t nStartSeconds;
	time_t nEndSeconds;

	printf("Begin RandGen. Data length :%ld \n", ulDataLen);
	nStartSeconds = time(NULL);

	ulRslt = SKF_GenRandom(hdev, pbData, ulDataLen);
	ERROR_THROW(ulRslt)

		nEndSeconds = time(NULL);
	printf("End....... Time interval:%ld s\n", nEndSeconds - nStartSeconds);

	printf("Data flow KB/s = %.2f\n\n", (float)ulDataLen / 1024 / (nEndSeconds - nStartSeconds));

END_OF_FUN:
	return ulRslt;
}


int sym_encrypt_decrypt_test(DEVHANDLE hdev, HCONTAINER hContainer, ULONG ulAlgID,
	BYTE *pbData, ULONG ulDataLen,
	BYTE *m_pbEncryptedData,
	BYTE *dec)
{
	ULONG ulRslt = SAR_OK;
	ECCPUBLICKEYBLOB ecc_pub = { 0 };
	BYTE Data[2048] = { 0 };
	HANDLE m_hkey = NULL;
	HANDLE m_hkey2 = NULL;
	BLOCKCIPHERPARAM EncryptParam = { 0 };
	time_t nStartSeconds;
	time_t nEndSeconds;
	ULONG m_ulEncryptedDatalen = MAXSIZE;
	ULONG decLen = MAXSIZE;


	ECCPUBLICKEYBLOB ecc_pub1 = { 0 };
	ECCPUBLICKEYBLOB ecc_pub2 = { 0 };
	ECCPUBLICKEYBLOB ecc_pub3 = { 0 };
	ECCPUBLICKEYBLOB ecc_pub4 = { 0 };
	ULONG ecc_pub3_len = sizeof(ECCPUBLICKEYBLOB);
	ULONG ecc_pub4_len = sizeof(ECCPUBLICKEYBLOB);
	BYTE eccCipher[1024] = { 0 };
	BYTE dec1[1024] = { 0 };
	ULONG decLen1 = 1024;

	//gen session hkey
	ulRslt |= SKF_GenECCKeyPair(hContainer, SGD_SM2_1, &ecc_pub1);
	ulRslt |= CertRequest_ecc(hContainer, "cont24", "1234", ecc_pub1);
	ulRslt |= SKF_ExportPublicKey(hContainer, TRUE, &ecc_pub3, &ecc_pub3_len);
	ulRslt |= SKF_ExportPublicKey(hContainer, FALSE, &ecc_pub4, &ecc_pub4_len);
	ulRslt |= SKF_ExtECCEncrypt(hdev, &ecc_pub4, "12345678", 8, (PECCCIPHERBLOB)eccCipher);
	ulRslt |= SKF_ECCPrvKeyDecrypt(hContainer, (PECCCIPHERBLOB)eccCipher, dec1, &decLen1);
	ERROR_THROW(ulRslt);
	
	ulRslt = SKF_ECCExportSessionKey(hContainer, ulAlgID, &ecc_pub4, (PECCCIPHERBLOB)Data, &m_hkey);
	ERROR_THROW(ulRslt);

	//根据加密模式设定IV
	memcpy(EncryptParam.IV, "1234567812345678", 16);
	EncryptParam.IVLen = 16;
	EncryptParam.PaddingType = 0;

	//enc
	ulRslt = SKF_EncryptInit(m_hkey, EncryptParam);
	ERROR_THROW(ulRslt);

	if (ulAlgID == SGD_SM1_ECB)
		printf("Begin encrypt data. Alog mod SGD_SM1_ECB; Data length :%ld \n", ulDataLen);
	if (ulAlgID == SGD_SM1_CBC)
		printf("Begin encrypt data. Alog mod SGD_SM1_CBC; Data length :%ld \n", ulDataLen);
	if (ulAlgID == SGD_SM4_ECB)
		printf("Begin encrypt data. Alog mod SGD_SM4_ECB; Data length :%ld \n", ulDataLen);
	if (ulAlgID == SGD_SM4_CBC)
		printf("Begin encrypt data. Alog mod SGD_SM4_CBC; Data length :%ld \n", ulDataLen);
	nStartSeconds = time(NULL);

	ulRslt = SKF_Encrypt(m_hkey, pbData, ulDataLen, m_pbEncryptedData, &m_ulEncryptedDatalen);
	ERROR_THROW(ulRslt);

	nEndSeconds = time(NULL);
	printf("End....... Time interval:%ld s\n", nEndSeconds - nStartSeconds);
	printf("Data flow KB/s = %.2f\n\n", (float)ulDataLen / 1024 / (nEndSeconds - nStartSeconds));

	//gen session hkey
	ulRslt = SKF_ImportSessionKey(hContainer, ulAlgID, (PECCCIPHERBLOB)Data, 0, &m_hkey2);
	ERROR_THROW(ulRslt);

	//dec
	ulRslt = SKF_DecryptInit(m_hkey2, EncryptParam);
	ERROR_THROW(ulRslt);

	if (ulAlgID == SGD_SM1_ECB)
		printf("Begin decrypt data. Alog mod SGD_SM1_ECB; Data length :%ld \n", ulDataLen);
	if (ulAlgID == SGD_SM1_CBC)
		printf("Begin decrypt data. Alog mod SGD_SM1_CBC; Data length :%ld \n", ulDataLen);
	if (ulAlgID == SGD_SM4_ECB)
		printf("Begin decrypt data. Alog mod SGD_SM4_ECB; Data length :%ld \n", ulDataLen);
	if (ulAlgID == SGD_SM4_CBC)
		printf("Begin decrypt data. Alog mod SGD_SM4_CBC; Data length :%ld \n", ulDataLen);
	nStartSeconds = time(NULL);

	ulRslt = SKF_Decrypt(m_hkey2, m_pbEncryptedData, m_ulEncryptedDatalen, dec, &decLen);
	ERROR_THROW(ulRslt);

	nEndSeconds = time(NULL);
	printf("End....... Time interval:%ld s\n", nEndSeconds - nStartSeconds);

	printf("Data flow KB/s = %.2f\n\n", (float)ulDataLen / 1024 / (nEndSeconds - nStartSeconds));


END_OF_FUN:
	return ulRslt;
}

int gen_sm2_keypair_test(HCONTAINER hContainer, ULONG num)
{
	ULONG ulRslt = SAR_OK;
	ECCPUBLICKEYBLOB ecc_pub = { 0 };
	time_t nStartSeconds;
	time_t nEndSeconds;

	printf("Begin generate sm2 key pairs...... \n");

	for (int i = 0; i < num; i++)
	{
		ECCPUBLICKEYBLOB ecc_pub = { 0 };
		ulRslt = SKF_GenECCKeyPair(hContainer, SGD_SM2_1, &ecc_pub);
		ERROR_THROW(ulRslt);

		printf("Successful - %d\n", i);
	}




END_OF_FUN:
	return ulRslt;
}


int sm2_encrypt_decrypt_test(ULONG dataLen)
{
	ULONG ulRslt = 0;
	SM2_PriKey pPriKey;
	SM2_PubKey pPubKey;
	UINT8 d[32] = { 0 };
	UINT8 x[32] = { 0 };
	UINT8 y[32] = { 0 };
	BYTE plain[2048] = { 0 };
	BYTE cipher[2048] = { 0 };
	BYTE dec[2048] = { 0 };
	ULONG cipherlen = sizeof(cipher);
	ULONG decLen = sizeof(dec);
	int times = 10;
	time_t nStartSeconds;
	time_t nEndSeconds;

	pPriKey.PrivateKey = (UINT32 *)d;
	pPubKey.X = (UINT32 *)x;
	pPubKey.Y = (UINT32 *)y;
	memset(plain, 0x11, sizeof(plain));

	//gen key
	ulRslt = ALGSm2_PR_GenKey(&pPriKey, &pPubKey, 256, 0);
	ERROR_THROW(ulRslt);

	//enc
	printf("Begin sm2 encrypt, Data length :%ld \n", dataLen);

	nStartSeconds = time(NULL);
	for (int i = 0; i < times; i++)
	{
		ulRslt = ALGSm2_PR_PubKeyEncrypt(&pPubKey, cipher, &cipherlen, plain, dataLen);
		ERROR_THROW(ulRslt);
	}
	nEndSeconds = time(NULL);

	printf("Successful 0 - %d\n", times - 1);
	printf("End....... Time interval:%ld s\n", nEndSeconds - nStartSeconds);
	printf("Data flow KB/s = %.2f\n\n", (float)dataLen * times / 1024 / ((nEndSeconds - nStartSeconds)));

	//dec
	printf("Begin sm2 decrypt, Data length :%ld \n", cipherlen);

	nStartSeconds = time(NULL);
	for (int i = 0; i < times; i++)
	{
		ulRslt = ALGSm2_PR_PriKeyDecrypt(&pPriKey, dec, &decLen, cipher, cipherlen);
		ERROR_THROW(ulRslt);
	}
	nEndSeconds = time(NULL);

	printf("Successful 0 - %d\n", times - 1);
	printf("End....... Time interval:%ld s\n", nEndSeconds - nStartSeconds);
	printf("Data flow KB/s = %.2f\n\n", (float)cipherlen * times / 1024 / ((nEndSeconds - nStartSeconds)));

END_OF_FUN:
	return ulRslt;
}

int sm2_sign_verify_test(HANDLE hdev, HANDLE hcont, ULONG times)
{
	ULONG ulRslt = SAR_OK;
	BYTE pbData[32] = { 0 };
	memset(pbData, 0x11, 32);
	time_t nStartSeconds;
	time_t nEndSeconds;
	ECCSIGNATUREBLOB SignData = { 0 };
	ECCPUBLICKEYBLOB ecc_pub = { 0 };

	//gen sm2 keypair
	ulRslt = SKF_GenECCKeyPair(hcont, SGD_SM2_1, &ecc_pub);
	ERROR_THROW(ulRslt);


	//sign
	printf("Begin sm2 signature...... \n");

	nStartSeconds = time(NULL);
	for (int i = 0; i < times; i++)
	{
		ulRslt = SKF_ECCSignData(hcont, pbData, 32, &SignData);
		ERROR_THROW(ulRslt);
	}
	nEndSeconds = time(NULL);

	printf("Successful 0 - %d\n", times - 1);
	printf("End....... Time interval:%ld s\n", nEndSeconds - nStartSeconds);
	printf("TPS = %.2f\n\n", (float)times / (nEndSeconds - nStartSeconds));


	//verify
	printf("Begin sm2 verify...... \n");

	nStartSeconds = time(NULL);
	for (int i = 0; i < times; i++)
	{
		ulRslt = SKF_ECCVerify(hdev, &ecc_pub, pbData, 32, &SignData);
		ERROR_THROW(ulRslt);
	}
	nEndSeconds = time(NULL);

	printf("Successful 0 - %d\n", times - 1);
	printf("End....... Time interval:%ld s\n", nEndSeconds - nStartSeconds);
	printf("TPS = %.2f\n\n", (float)times / (nEndSeconds - nStartSeconds));

END_OF_FUN:
	return ulRslt;
}

int sm3_digest_test(HANDLE hdev, BYTE *inData, ULONG dataLen)
{
	ULONG ulRslt = 0;
	HANDLE hHash = 0;
	BYTE outData[64] = { 0 };
	ULONG outDataLen = sizeof(outData);
	time_t nStartSeconds;
	time_t nEndSeconds;

	//sm3 digest init
	ulRslt = SKF_DigestInit(hdev, SGD_SM3, NULL, NULL, 0, &hHash);
	ERROR_THROW(ulRslt);

	//sm3 digest
	printf("Begin sm3 digest...... \n");
	nStartSeconds = time(NULL);

	ulRslt = SKF_Digest(hHash, inData, dataLen, outData, &outDataLen);
	ERROR_THROW(ulRslt);

	nEndSeconds = time(NULL);
	printf("End....... Time interval:%ld s\n", nEndSeconds - nStartSeconds);

	printf("Data flow KB/s = %.2f\n\n", (float)dataLen / 1024 / (nEndSeconds - nStartSeconds));

END_OF_FUN:
	return ulRslt;
}

//设备认证
static ULONG DevAuth(DEVHANDLE hDev)
{
	ULONG rv = 0;
	sm4_context ctx = { 0 };
	BYTE key[256] = "1234567812345678";
	BYTE pbRandom[32] = { 0 };
	BYTE szEncryptedData[256] = { 0 };
	ULONG ulEncryptedDataLen = 16;

	rv |= SKF_GenRandom(hDev, pbRandom, 8);

	sm4_setkey_enc(&ctx, key);
	sm4_crypt_ecb(&ctx, SM4_ENCRYPT, 16, pbRandom, szEncryptedData);

	rv |= SKF_DevAuth(hDev, szEncryptedData, ulEncryptedDataLen);

	return rv;
}


static ULONG SKF_function_test_init1(DEVHANDLE *phDev, HAPPLICATION *phApplication, HCONTAINER *phContainer1, HCONTAINER *phContainer2)
{
	ULONG rv = 0;
	CHAR DevName[256] = { 0 };
	ULONG DevNameLen = 256;
	CHAR AppName[256] = { 0 };
	ULONG AppNameLen = 256;
	ULONG RetryCount = 0;
	CHAR ContName[256] = { 0 };
	ULONG ContNameLen = 256;

	//枚举 连接设备
	rv = SKF_EnumDev(TRUE, DevName, &DevNameLen);
	if (rv) goto END;

	rv = SKF_ConnectDev(DevName, phDev);
	if (rv) goto END;

	//格式化设备，设置设备认证秘钥
	rv = SKF_FormatDev(*phDev);
	if (rv) goto END;

	//获得应用句柄
	rv = SKF_EnumApplication(*phDev, AppName, &AppNameLen);
	if (rv) goto END;

	if (AppNameLen == 0)
	{
		//设备认证
		rv = DevAuth(*phDev);
		if (rv) goto END;

		//创建应用
		rv = SKF_CreateApplication(*phDev, "app_vmkey", "admin", 10, "12345678", 10, 0xFF, phApplication);
		if (rv) goto END;
	}
	else
	{
		rv = SKF_OpenApplication(*phDev, AppName, phApplication);
		if (rv) goto END;
	}

	rv = SKF_VerifyPIN(*phApplication, USER_TYPE, (char *)"12345678", &RetryCount);
	if (rv) goto END;


	//创建两个容器用于测试密钥协商
	rv = SKF_OpenContainer(*phApplication, "cont1_vmkey", phContainer1);
	if (rv)
	{
		rv = SKF_CreateContainer(*phApplication, "cont1_vmkey", phContainer1);
		if (rv) goto END;
	}

	rv = SKF_OpenContainer(*phApplication, "cont2_vmkey", phContainer2);
	if (rv)
	{
		rv = SKF_CreateContainer(*phApplication, "cont2_vmkey", phContainer2);
		if (rv) goto END;
	}

END:
	return rv;
}

static ULONG SKF_function_test_init(DEVHANDLE *phDev, HAPPLICATION *phApplication, HCONTAINER *phContainer1, HCONTAINER *phContainer2)
{
	ULONG rv = 0;
	CHAR DevName[256] = { 0 };
	ULONG DevNameLen = 256;
	CHAR AppName[256] = { 0 };
	ULONG AppNameLen = 256;
	ULONG RetryCount = 0;
	CHAR ContName[256] = { 0 };
	ULONG ContNameLen = 256;

	//枚举连接设备
	rv = SKF_EnumDev(TRUE, DevName, &DevNameLen);
	if (rv) goto END;

	rv = SKF_ConnectDev(DevName, phDev);
	if (rv) goto END;


	//获得应用句柄
	rv = SKF_EnumApplication(*phDev, AppName, &AppNameLen);
	if (rv) goto END;

	rv = SKF_OpenApplication(*phDev, AppName, phApplication);
	if (rv) goto END;

	//验证用户pin
	rv = SKF_VerifyPIN(*phApplication, USER_TYPE, (char *)"12345678", &RetryCount);
	if (rv) goto END;

	//获得容器句柄
	rv = SKF_EnumContainer(*phApplication, ContName, &ContNameLen);
	if (rv) goto END;

	rv = SKF_OpenContainer(*phApplication, ContName, phContainer1);
	if (rv) goto END;

END:
	return rv;
}

static int dev_init(DEVHANDLE *phDev, HAPPLICATION *phApplication, HCONTAINER *phContainer1, HCONTAINER *phContainer2)
{
	ULONG rv = 0;
	CHAR DevName[256] = { 0 };
	ULONG DevNameLen = sizeof(DevName);
	CHAR AppName[256] = { 0 };
	ULONG AppNameLen = sizeof(AppName);
	CHAR ContName[256] = { 0 };
	ULONG ContNameLen = sizeof(ContName);
	ULONG RetryCount = 0;
	DEVINFO DevInfo = {0};

	//枚举设备
	rv = SKF_EnumDev(TRUE, DevName, &DevNameLen);
	if (rv) goto END;
	//连接设备
	rv = SKF_ConnectDev(DevName, phDev);
	if (rv) goto END;
	//获取设备信息
	rv = SKF_GetDevInfo(*phDev, &DevInfo);
	if (rv) goto END;
	//枚举应用
	rv = SKF_EnumApplication(*phDev, AppName, &AppNameLen);
	if (rv) goto END;

	if (AppNameLen == 0)
	{
		//设备认证
		rv = DevAuth(*phDev);
		if (rv) goto END;
		//创建应用
		rv = SKF_CreateApplication(*phDev, "app_vmkey", "admin", 10, "12345678", 10, 0xFF, phApplication);
		if (rv) goto END;
	}
	else
	{
		//打开应用
		rv = SKF_OpenApplication(*phDev, AppName, phApplication);
		if (rv) goto END;
	}

	//验证用户pin
	rv = SKF_VerifyPIN(*phApplication, USER_TYPE, (char *)"12345678", &RetryCount);
	if (rv) goto END;

	//枚举容器
	rv = SKF_EnumContainer(*phApplication, ContName, &ContNameLen);
	if (rv) goto END;

	if (ContNameLen == 0)
	{
		//创建容器
		rv = SKF_CreateContainer(*phApplication, "cont1_vmkey", phContainer1);
		if (rv) goto END;
	}
	else
	{
		//打开容器
		rv = SKF_OpenContainer(*phApplication, ContName, phContainer1);
		if (rv) goto END;
	}

END:
	return rv;
}

static void MFS_FLASH_gen()
{
	char *g_FlashPath = "D:\\Program Files (x86)\\longmai_vmkey_service\\vmkey_client\\11\\vmkey_test_data\\client_0000\\MFS.FLASH";
	FILE *fp = NULL;
	char pData[1024] = { 0 };
	int nTotalSize = 256;
	int i = 0;

	fp = fopen(g_FlashPath, "w+");
	if (fp != NULL)
	{
		printf("fopen ok\n");
		memset(pData, 0xff, 1024);
		for (i = 0; i<nTotalSize; i++)
		{
			fwrite(pData, 1024, 1, fp);
		}
		fclose(fp);
		fp = NULL;
		printf("fwrite ok\n");
	}
}



int DEVEFlash_PR_Confusion(char *src, char *dest)
{
	FILE *fp_src = NULL;
	FILE *fp_dest = NULL;
	char  data_src[1024*256] = { 0 };
	char data_dest[1024*256] = { 0 };
	int i = 0;
	int ret = 0;

	fp_src = fopen(src, "rb");
	fp_dest = fopen(dest, "wb");

	ret = fread(data_src, 1024 * 256, 1, fp_src);

	for (i = 0; i < 1024 * 256; i++)
		data_dest[i] = data_src[i] ^ 0x6A;

	ret = fwrite(data_dest, 1024 * 256, 1, fp_dest);
	
	fclose(fp_src);
	fclose(fp_dest);

	fp_src = NULL;
	fp_dest = NULL;
		
	return 0;
}


int DEVEFlash_PR_Confusion1(char *dest)
{
	FILE *fp = NULL;
	UINT32 pData[1024] = { 0 };
	int i = 0;

	fp = fopen(dest, "rb+");
	if (fp == NULL)
	{
		fp = fopen(dest, "w+");
	}

	memset(pData, 0x6A, 1024);
	for (i = 0; i < 256; i++)
	{
		fwrite(pData, 1024, 1, fp);
	}


	fclose(fp);
	fp = NULL;
	return (0);
}


#define CERT_CONF_FLASH_PATH "D:\\Program Files (x86)\\longmai_vmkey_service\\vmkey_client\\11\\vmkey_test_data\\client_0000"
int main()
{
	printf("Longmai tech SKF Interface Test\n");
	int ret = 0;
	DEVHANDLE hdev = NULL;
	HAPPLICATION happ = NULL;
	HCONTAINER hcont1 = NULL;
	HCONTAINER hcont2 = NULL;

	BYTE *testData = NULL;
	ULONG OutData1Len = MAXSIZE;
	ULONG OutData2Len = MAXSIZE;

	testData = (BYTE *)malloc(MAXSIZE * sizeof(BYTE));
	if (testData == NULL)
	{
		printf("Out of memory!\n");
		exit(1);
	}
	memset(testData, 0x11, MAXSIZE * sizeof(BYTE));

	
	//配置文件修改
	/*
	ret |= write_conf_vmkey_string("VMKEY", "ip", "192.168.1.81", CONF_NAME);
	ret |= write_conf_vmkey_int("VMKEY", "port", 8019, CONF_NAME);
	ret |= write_conf_vmkey_int("VMKEY", "ssltype", 0, CONF_NAME);
	ret |= write_conf_vmkey_int("VMKEY", "timeout", 60, CONF_NAME);
	ret |= write_conf_vmkey_int("VMKEY", "loglevel", 2, CONF_NAME);

	ret |= write_conf_vmkey_string("CA", "ip_ca", "192.168.0.49", CONF_NAME);
	ret |= write_conf_vmkey_int("CA", "port_ca", 10000, CONF_NAME);
	ERROR_THROW(ret)
	*/
	
	//vmkey初始化
	ret = Init_VMKey(CERT_CONF_FLASH_PATH, "SerialNumber001");
	ERROR_THROW(ret);

	//vmkey init
	ret = dev_init(&hdev, &happ, &hcont1, &hcont2);
	ERROR_THROW(ret);

	printf("\nUKEY INIT OK!!! \n");
	printf("***************************************************************\n");

#if 0

	ECCPUBLICKEYBLOB ecc_pub1 = { 0 };
	ECCPUBLICKEYBLOB ecc_pub2 = { 0 };
	ECCPUBLICKEYBLOB ecc_pub3 = { 0 };
	ECCPUBLICKEYBLOB ecc_pub4 = { 0 };
	ULONG ecc_pub3_len = sizeof(ECCPUBLICKEYBLOB);
	ULONG ecc_pub4_len = sizeof(ECCPUBLICKEYBLOB);
	BYTE eccCipher[1024] = { 0 };
	BYTE dec[1024] = { 0 };
	ULONG decLen = 1024;

	ret |= SKF_GenECCKeyPair(hcont1, SGD_SM2_1, &ecc_pub1);
	ret |= CertRequest_ecc(hcont1, "cert001", "001", ecc_pub1);
	ret |= SKF_ExportPublicKey(hcont1, TRUE, &ecc_pub3, &ecc_pub3_len);
	ret |= SKF_ExportPublicKey(hcont1, FALSE, &ecc_pub4, &ecc_pub4_len);

	//sm2加解密
	ret |= SKF_ExtECCEncrypt(hdev, &ecc_pub4, "12345678", 8, (PECCCIPHERBLOB)eccCipher);
	ret |= SKF_ECCPrvKeyDecrypt(hcont1, (PECCCIPHERBLOB)eccCipher, dec, &decLen);
	ERROR_THROW(ret);

	////////////////////////////////////////////////////////////////////////////////////////////
	//50MB
	//ret = gen_random_test(hdev, testData, 256 * B);
	//ERROR_THROW(ret)

	//	ret = gen_sm2_keypair_test(hcont1, 100);
	ERROR_THROW(ret);

	/*
	//cert request
	ECCPUBLICKEYBLOB pub_ecc = {0};
	int CertRequest_ecc(HCONTAINER m_hcout, char *pszCommonName, char *pszSeriName, ECCPUBLICKEYBLOB pub_ecc);
	ulRslt = CertRequest_ecc(hcont, "cont1", "123", pub_ecc);
	*/


	//128B
	ret = sm2_encrypt_decrypt_test(128 * B);
	ERROR_THROW(ret);

	//10次
	ret = sm2_sign_verify_test(hdev, hcont1, 10);
	ERROR_THROW(ret);


#endif

	//50MB
	//ret = sm3_digest_test(hdev, testData, 256 * B);
	//ERROR_THROW(ret);

	//50MB
	ret = sym_encrypt_decrypt_test(hdev, hcont1, SGD_SM4_ECB, testData, 256 * B, testData, testData);
	ERROR_THROW(ret);
	ret = sym_encrypt_decrypt_test(hdev, hcont1, SGD_SM4_CBC, testData, 256 * B, testData, testData);
	ERROR_THROW(ret);


END_OF_FUN:
	free(testData);
	testData = NULL;

	if (hcont1)
		SKF_CloseContainer(hcont1);
	if (hcont2)
		SKF_CloseContainer(hcont2);
	if (happ)
		SKF_CloseApplication(happ);
	if (hdev)
		SKF_DisConnectDev(hdev);

	printf("\nTest Finished, ErrorCode = 0X%08X \n", ret);

	getchar();
	return 0;
}

#endif

#if 0

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


static void itemtrim(char *data)
{
	int i, len;
	len = strlen(data);
	for (i = 0; i < len; i++)
	{
		if (data[i] == '\r' || data[i] == '\n')
		{
			data[i] = 0x0;
			return;
		}
	}
}
static int getitemvalue(char *filename, char *itemname, char *itemvalue)
{
	FILE *fp = NULL;
	char sLine[1024] = {0};
	char *value = NULL;
	
	if (NULL == (fp = fopen(filename, "r")))
	{
		return -1;
	}

	while (!feof(fp))
	{
		fgets(sLine, 1024, fp);
		if (strstr(sLine, itemname) == sLine)//该函数返回 str1字符串从 str2第一次出现的位置开始到 str1结尾的字符串
		{
			value = strstr(sLine, "=");
			
			//去等号
			value++;

			//去换行
			value[strlen(value) - 1] = '\0';
			
			strcpy(itemvalue, value);

			fclose(fp);
			return 0;
		}
	}

	fclose(fp);
	return -1;
}


#define CONF_NAME "C:\\Users\\jpx\\Desktop\\vmkey_client\\vmkey_test_data\\client_0000\\conf.ini"
int main()
{
	int ret = 0;
	char value1[127] = {0};
	char value2[127] = { 0 };
	char value3[127] = { 0 };

	ret = getitemvalue(CONF_NAME, "ip", value1);
	ret = getitemvalue(CONF_NAME, "port_ca", value2);
	ret = getitemvalue(CONF_NAME, "ca", value3);

	getchar();


}

#endif

#if 1

#include <stdio.h>



int main()
{
	char s[64] = {0};
	char *devID1 = "48f95a894730f0aeac5978fef9d540d269265975a9199cfe3a08f98baa9f3360";
	int len = strlen(devID1);

	strncpy(s, devID1, strlen(devID1));
	

	 len = strlen(s);

	int ret = 0;
	char devID[64] = { 0 };
	unsigned char features[32] = {
		0x48, 0xf9, 0x5a, 0x89, 0x47, 0x30, 0xf0, 0xae, 0xac, 0x59, 0x78, 0xfe, 0xf9, 0xd5, 0x40, 0xd2,
		0x69, 0x26, 0x59, 0x75, 0xa9, 0x19, 0x9c, 0xfe, 0x3a, 0x08, 0xf9, 0x8b, 0xaa, 0x9f, 0x33, 0x60
	};

	for (int i = 0; i < 32; i++)
	{
		sprintf(&devID[2*i], "%02x", features[i]);
	}

	getchar();
}

#endif

